package com.example.s.Activity.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.s.Activity.BaseActivity;
import com.example.s.Activity.login.LoginActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.utils.CacheUtil;
import com.example.s.view.MyDialog;

public class SettingActivity extends BaseActivity {
    private ImageView back;
    private RelativeLayout accountAndSecurity;
    private RelativeLayout delCache;
    private RelativeLayout about;
    private RelativeLayout feedback;
    private Button exit;
    private TextView cacheSize;


    private void initView() {

        back = (ImageView) findViewById(R.id.back);
        accountAndSecurity = (RelativeLayout) findViewById(R.id.accountAndSecurity);
        delCache = (RelativeLayout) findViewById(R.id.delCache);
        about = (RelativeLayout) findViewById(R.id.about);
        feedback = (RelativeLayout) findViewById(R.id.feedback);
        exit = (Button) findViewById(R.id.exit);

        cacheSize = (TextView) findViewById(R.id.cacheSize);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        try {
            cacheSize.setText(CacheUtil.getInstance().getTotalCacheSize(SettingActivity.this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        accountAndSecurity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, AccountAndSecurity.class));
            }
        });

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
            }
        });

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, FeedbackActivity.class));
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this,LoginActivity.class));
                finish();
            }
        });
        /**
         * 清楚缓存
         */
        final MyDialog[] dialog = new MyDialog[1];
        delCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog[0] = new MyDialog(SettingActivity.this, new MyDialog.OnMyDialogListener() {
                    @Override
                    public void handleDialog(InnerHolder holder) {
                        holder.setText(R.id.title, "确认清除缓存?")
                                .setOnClickListener(R.id.confirm, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        CacheUtil.getInstance().clearAllCache(SettingActivity.this);
                                        cacheSize.setText("0 K");
                                        Toast.makeText(SettingActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
                                        dialog[0].disMyDialog();
                                    }
                                });
                    }
                });


            }
        });
    }

}
